# 自动化测试最重要的事情之一： 初始化和清除 ，初始化就是准备测试用例所需要的各种条件


from hytest import *
from lib.webui import *
from selenium.webdriver.common.by import By
import time



def suite_setup():
    wd = open_browser()
    GSTORE['wd'] = wd
    login(wd)


def suite_teardown():
    wd = GSTORE['wd']
    wd.quit()

class UI0101:
    name = '检查侧边栏是否显示正确'

    def setup(self):
        pass

    def teardown(self):
        pass

    def teststeps(self):
        wd = GSTORE['wd']
       
        STEP(1,'检查侧边栏')
        ui0101_ele =  wd.find_elements(By.XPATH,"//ul[@class='sidebar-menu tree']/li[@class='header']/following-sibling::*[position()<=3]")
        
        CHECK_POINT('前三项菜单名称分别为：客户,药品,订单',ui0101_ele[0].text=='客户' and ui0101_ele[1].text == '药品' and ui0101_ele[2].text =='订单')
        

class UI0102:
    name = '检查客户添加是否成功'

    def setup(self):
        INFO('添加客户')
        wd = GSTORE['wd']
        wd.find_element(By.CSS_SELECTOR,"ul[class='sidebar-menu tree'] li:nth-of-type(2)").click()
        wd.find_element(By.CSS_SELECTOR,'button[type]').click()
        ele_ments = wd.find_elements(By.CSS_SELECTOR,'section input')
        ele_ments[0].send_keys('南京中医院')
        ele_ments[1].send_keys('18628904684')
        wd.find_element(By.CSS_SELECTOR,'section textarea').send_keys('南京省武汉路')
        wd.find_element(By.CSS_SELECTOR,"div[class='col-lg-12 col-md-12 col-sm-12']>button:nth-of-type(1)").click()
        time.sleep(1)

    def teardown(self):
        INFO(f'删除UI0102添加的客户')
        wd = GSTORE['wd']
        ele = wd.find_elements(By.CSS_SELECTOR,'.search-result-item')[0]
        ele.find_element(By.CSS_SELECTOR,'label:nth-child(even)').click()
        time.sleep(1)
        wd.switch_to.alert.accept()

    def teststeps(self):
        STEP(1,'检查客户信息')
        wd = GSTORE['wd']
        ele_rs = wd.find_elements(By.CSS_SELECTOR,'.search-result-item')
        fields = ele_rs[0].find_elements(By.CSS_SELECTOR,'div span:nth-child(even)')
        texts = [field.text for field in fields]
        expect_rs = ['南京中医院','18628904684','南京省武汉路']
        CHECK_POINT('检查客户是否添加成功,信息是否正确',expect_rs ==texts)

class UI0103:
    name = '检查编辑功能'

    def setup(self):
        INFO('添加客户')
        wd = GSTORE['wd']
        wd.find_element(By.CSS_SELECTOR,"ul[class='sidebar-menu tree'] li:nth-of-type(2)").click()
        wd.find_element(By.CSS_SELECTOR,'button[type]').click()
        ele_ments = wd.find_elements(By.CSS_SELECTOR,'section input')
        ele_ments[0].send_keys('南京中医院')
        ele_ments[1].send_keys('18628904684')
        wd.find_element(By.CSS_SELECTOR,'section textarea').send_keys('南京省武汉路')
        wd.find_element(By.CSS_SELECTOR,"div[class='col-lg-12 col-md-12 col-sm-12']>button:nth-of-type(1)").click()
        time.sleep(1)
    
    def teardown(self):
        INFO(f'删除UI0103添加的客户')
        wd = GSTORE['wd']
        ele = wd.find_elements(By.CSS_SELECTOR,'.search-result-item')[0]
        ele.find_element(By.CSS_SELECTOR,'label:nth-child(even)').click()
        time.sleep(1)
        wd.switch_to.alert.accept()

    def teststeps(self):
        STEP(1,'点击编辑按键，修改信息')
        wd = GSTORE['wd']
        rs_ele = wd.find_element(By.CSS_SELECTOR,'div[class="search-result-item"] label:nth-child(1)')
        rs_ele.click()
        ele_ments_0103 = wd.find_elements(By.CSS_SELECTOR,'div[class="search-result-item"] input')
        ele_ments_0103[0].clear()
        ele_ments_0103[0].send_keys('南京省中医院')
        ele_ments_0103[1].clear()
        ele_ments_0103[1].send_keys('26558822')
        wd.find_element(By.XPATH,'//div[@class="search-result-item"] [1]//label[1]').click()
        time.sleep(1)
        STEP(2,'检查编辑的客户信息')
        wd = GSTORE['wd']
        ele_rs = wd.find_elements(By.CSS_SELECTOR,'.search-result-item')
        fields = ele_rs[0].find_elements(By.CSS_SELECTOR,'div span:nth-child(even)')
        texts = [field.text for field in fields]
        expect_rs = ['南京省中医院','26558822','南京省武汉路']
        CHECK_POINT('检查编辑的客户是否修改成功,信息是否正确',expect_rs ==texts)
        